<!--  -->
<template>
  <div class="meedu-main-body">
    <BackBar class="mb-30" title="录播分类管理"></BackBar>
    <div class="float-left mb-30">
      <el-button
        @click="$router.push({ name: 'CategoriesCreate' })"
        type="primary"
        >添加分类
      </el-button>
    </div>
    <div class="float-left">
      <div class="float-left">
        <el-table
          :header-cell-style="{ background: '#f1f2f9' }"
          :data="categories"
          row-key="id"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          class="float-left"
        >
          <el-table-column prop="id" label="ID" width="150"> </el-table-column>
          <el-table-column prop="sort" label="升序" width="150">
          </el-table-column>

          <el-table-column label="分类名"
            ><template slot-scope="scope">
              <span>{{ scope.row.name }} </span>
            </template>
          </el-table-column>
          <el-table-column label="显示" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.is_show == 1">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="160">
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="
                  $router.push({
                    name: 'CategoriesUpdate',
                    query: { id: scope.row.id },
                  })
                "
                >编辑</el-link
              >
              <el-link class="ml-5" type="danger" @click="destory(scope.row.id)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="float-left mt-30 text-center">
        <el-pagination
          @size-change="paginationSizeChange"
          @current-change="paginationPageChange"
          :current-page="pagination.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BackBar from "@/components/back-bar";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { BackBar },

  data() {
    return {
      pageName: "vodCategory-list",
      pagination: {
        page: 1,
        size: 10,
      },
      total: 0,
      loading: false,
      categories: [],
      userRemark: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getCategories() {
      if (this.loading) return;
      this.loading = true;
      let params = {};
      Object.assign(params, this.pagination);
      this.$request.get(`courseCategory`, params).then((res) => {
        this.loading = false;
        this.categories = res.data.data;
        this.total = res.data.total;
      });
    },
    paginationSizeChange(size) {
      this.pagination.size = size;
      this.getCategories();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getCategories();
    },
    paginationReset() {
      this.pagination.page = 1;
      this.getCategories();
    },
    destory(id) {
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定按钮的操作
          if (this.loading) return;
          this.loading = true;
          this.$request
            .delete(`courseCategory/${id}`)
            .then(() => {
              this.loading = false;
              this.$message.success(this.$t("common.success"));
              this.paginationReset();
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {
          //点击删除按钮的操作
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getCategories();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
</style>